package edu.rjxy.xjdx.emps.servlet;

import edu.rjxy.xjdx.emps.service.UserService;
import edu.rjxy.xjdx.emps.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    //控制层 注入 服务层
    UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String loginName = request.getParameter("loginName").trim();// admin
        String password = request.getParameter("password").trim(); // admin
        int rs = userService.login(loginName, password);
        String path = "/pages/login.jsp";
        if(rs==1){
            request.getSession().setAttribute("current_user",loginName);
            path="/pages/main.jsp";
        }
        request.setAttribute("msg",rs==1?("欢迎您，"+loginName):(rs==-1?"用户名不存在":"密码错误"));
        request.getRequestDispatcher(path).forward(request,response);
    }
}
/*      //  String msg = null;
 *//* if(rs==1){
            path="/pages/main.jsp";
            msg="欢迎您，"+loginName;
        }else if(rs==-1){
            msg="用户名不存在";
        }else if (rs==0){
            msg="密码错误";
        }*//*
//        request.setAttribute("msg",msg);*/